/*
** monitor.c for zapppy in /home/druart_p/Work/projet/zappy/zappy-2015-2014s-renard_g/serv-src
**
** Made by pierrick druart
** Login   <druart_p@epitech.net>
**
** Started on  Fri Jun 22 15:12:58 2012 pierrick druart
** Last update Sun Jul 15 21:47:09 2012 maximilien addari
*/

#include		<string.h>
#include		<stdlib.h>

#include		"server.h"

static t_monitor	ptr_monitor[]=
  {
    {"msz", &msz},
    {"bct", &bct},
    {"mct", &mct},
    {"tna", &tna},
    {"plv", &plv},
    {"pin", &pin},
    {"sgt", sgt},
    {"sst", &sst},
    {NULL, 0}
  };

void			cmd_monitor(char *cmd, t_client *client, t_game *game)
{
  int			i;
  int			found;

  i = 0;
  found = 0;
  while (ptr_monitor[i].cmd)
    {
      if (!strncmp(cmd, ptr_monitor[i].cmd, strlen(ptr_monitor[i].cmd)))
	{
	ptr_monitor[i].cmdfct(client, game, cmd);
	found = 1;
	}
      i++;
    }
  if (!found)
    client->send->concat(client->send, "suc\n");
}

void			msz(t_client *client, t_game *game, char *cmd)
{
  (void)cmd;
  client->send->concat(client->send, "msz ");
  client->send->concat_value(client->send, game->coord.x);
  client->send->concat(client->send, " ");
  client->send->concat_value(client->send, game->coord.y);
  client->send->concat(client->send, "\n");
}

void			tna(t_client *client, t_game *game, char *cmd)
{
  struct s_item		*cur;
  struct s_team		*team;

  (void)cmd;
  cur = game->team->head;
  while (cur != NULL)
    {
      team = (struct s_team *) cur->content;
      client->send->concat(client->send, "tna ");
      client->send->concat(client->send, team->name);
      client->send->concat(client->send, "\n");
      cur = cur->next;
    }
}

void			sgt(t_client *client, t_game *game, char *cmd)
{
  (void)cmd;
  client->send->concat(client->send, "sgt ");
  client->send->concat_value(client->send, game->time);
  client->send->concat(client->send, "\n");
}

void			sst(t_client *client, t_game *game, char *cmd)
{
  char			*token;
  int			time;

  token = strtok(cmd, " ");
  if ((token = strtok(NULL, " ")) != NULL)
    {
      time = atoi(token);
      game->time = time;
      client->send->concat(client->send, "sgt ");
      client->send->concat_value(client->send, game->time);
      client->send->concat(client->send, "\n");
    }
  else
    client->send->concat(client->send, "sbp\n");
}
